package advs;

import net.jxta.document.Advertisement;
import net.jxta.document.AdvertisementFactory;
import net.jxta.document.Document;
import net.jxta.document.Element;
import net.jxta.document.ExtendableAdvertisement;
import net.jxta.document.MimeMediaType;
import net.jxta.document.StructuredDocument;
import net.jxta.document.StructuredDocumentFactory;
import net.jxta.id.ID;

public class ServicePublisherAdvertisement extends ExtendableAdvertisement
{
	String serviceName;
	String servicePath;
	String reqParam;
	String paramTypes;
	String id;
	String serviceType ;
	String pipeAdvID;
	String peerID;
	

	public static String ServiceNameTag = "ServiceName";
	public static String ServicePathTag = "ServicePath";
	public static String IDTag = "ID";
	public static String ReqParamTag = "ReqParam";
	public static String ParamTypesTag = "ParamType";
	public static String ServiceTypeTag = "ServiceType";
	public static String PipeAdvIDTag= "PipeAdvID";
	public static String PeerIDTag= "PeerID";

	String[] fields = new String[]
	{ ServiceNameTag, ServiceTypeTag ,IDTag};

	public ServicePublisherAdvertisement()
	{
		super();
	}

	@Override
	public String getBaseAdvType()
	{
		return ServicePublisherAdvertisement.getAdvertisementType();
	}

	@Override
	public ID getID()
	{
		return ID.nullID;
	}

	@Override
	public String[] getIndexFields()
	{
		return fields;
	}

	public static String getAdvertisementType()
	{
		return "solver:spa";
	}

	public static class Instantiator implements
			AdvertisementFactory.Instantiator
	{

		@Override
		public String getAdvertisementType()
		{
			return ServicePublisherAdvertisement.getAdvertisementType();
		}

		@Override
		public Advertisement newInstance()
		{
			return new ServicePublisherAdvertisement();
		}

		@Override
		public Advertisement newInstance(Element root)
		{
			ServicePublisherAdvertisement adv = new ServicePublisherAdvertisement();
			Element element = (Element) root.getChildren(ServiceNameTag)
					.nextElement();
			if (element != null && element.getValue() != null)
			{
				adv.setServiceName((String) element.getValue());
			}
			element = (Element) root.getChildren(ServicePathTag).nextElement();
			if (element != null && element.getValue() != null)
			{
				adv.setServicePath((String) element.getValue());
			}

			element = (Element) root.getChildren(ReqParamTag).nextElement();
			if (element != null && element.getValue() != null)
			{
				adv.setReqParam((String)element.getValue());
			}

			element = (Element) root.getChildren(ServiceTypeTag).nextElement();
			if (element != null && element.getValue() != null)
			{
				adv.setServiceType((String) element.getValue());
			}
			
			element = (Element) root.getChildren(PipeAdvIDTag).nextElement();
			if (element != null && element.getValue() != null)
			{
				adv.setPipeAdvID((String) element.getValue());
			}
			
			element = (Element) root.getChildren(IDTag).nextElement();
			if (element != null && element.getValue() != null)
			{
				adv.setId((String) element.getValue());
			}

			element = (Element) root.getChildren(PeerIDTag).nextElement();
			if (element != null && element.getValue() != null)
			{
				adv.setPeerID((String) element.getValue());
			}
			
			element = (Element) root.getChildren(ParamTypesTag).nextElement();
			if (element != null && element.getValue() != null)
			{
				adv.setParamTypes((String) element.getValue());
			}
			
			return adv;
		}

	}

	public Document getDocument(MimeMediaType mimeMediaType)
	{
		StructuredDocument doc = StructuredDocumentFactory
				.newStructuredDocument(mimeMediaType, getAdvertisementType());
		Element element = doc.createElement(ServiceNameTag, serviceName);
		doc.appendChild(element);
		element = doc.createElement(ServicePathTag, servicePath.toString());
		doc.appendChild(element);
		element = doc.createElement(IDTag, id);
		doc.appendChild(element);
		element = doc.createElement(ReqParamTag, reqParam);
		doc.appendChild(element);
		element = doc.createElement(ServiceTypeTag, serviceType);
		doc.appendChild(element);
		element = doc.createElement(PipeAdvIDTag, pipeAdvID);
		doc.appendChild(element);
		element = doc.createElement(PeerIDTag, peerID);
		doc.appendChild(element);		
		element = doc.createElement(ParamTypesTag, paramTypes);
		doc.appendChild(element);	
		return doc;
	}

	public String getServiceName()
	{
		return serviceName;
	}

	public void setServiceName(String serviceName)
	{
		this.serviceName = serviceName;
	}

	public String getServicePath()
	{
		return servicePath;
	}

	public void setServicePath(String servicePath)
	{
		this.servicePath = servicePath;
	}

	public String getReqParam()
	{
		return reqParam;
	}

	public void setReqParam(String reqParam)
	{
		this.reqParam = reqParam;
	}

	public String getId()
	{
		return id;
	}

	public void setId(String id)
	{
		this.id = id;
	}

	public String getServiceType()
	{
		return serviceType;
	}

	public void setServiceType(String serviceType)
	{
		this.serviceType = serviceType;
	}

	public String getPipeAdvID()
	{
		return pipeAdvID;
	}

	public void setPipeAdvID(String pipeAdvID)
	{
		this.pipeAdvID = pipeAdvID;
	}

	public String getPeerID()
	{
		return peerID;
	}

	public void setPeerID(String peerID)
	{
		this.peerID = peerID;
	}

	public String getParamTypes()
	{
		return paramTypes;
	}

	public void setParamTypes(String paramTypes)
	{
		this.paramTypes = paramTypes;
	}
}
